import json
import time


# 读取文件，支持类型：json,txt
def read_file(data, path):
    if path.endswith('.json'):
        return read_json_to_dict(path)
    if path.endswith('.txt'):
        return read_txt_to_dict(path)
    raise AttributeError('不支持读取该类型文件:' + path)


# 开启日志
def open_log(data):
    from tools.express.register import log
    log['open'] = True
    return data


# 关闭日志
def close_log(data):
    from tools.express.register import log
    log['open'] = False
    return data


def sleep(data, param='5'):
    seconds = int(param)
    time.sleep(seconds)
    return data


def read_json_to_dict(path, encoding='utf-8') -> dict:
    with open(path, 'r', encoding=encoding) as fp:
        return json.load(fp)


def read_txt_to_dict(path, encoding='utf-8') -> str:
    with open(path, 'r', encoding=encoding) as fp:
        return fp.read()


# 将键值对导出到文件中
def write_json_to_file(path, content, encoding='utf-8'):
    with open(path, 'w', encoding=encoding) as fp:
        json.dump(content, fp, ensure_ascii=False)
